*Calculating environmental abatement cost expenditure and investment data*


*************
*Please note that the final establishment-level datasets generated by this do file has already been converted into stata(.dta) files "new_miljoskydd_stack.dta". All the intermediate variables have been removed.
*************

*cd "/RFS_replication_package/Martinsson_et_al_datasets"



/*(Remove here to get the original codes used in the paper)



local a =2002

while `a'<2018 {
	use  "miljoskydd_`a'.dta" , clear
	
	display `a'
	g year_1=`a'
	sum year_1
	capture drop Ar
	capture drop _r
	 destring, replace
	
	*capture drop sni
	destring, replace
	g year=year_1
	drop year_1
	rename *, lower
	if year>=2015 & year <2017  	{ 
	g new_sni=real(sni) if real(sni)==1
	drop sni
	g sni=new_sni
	*drop new_sni
	}
	
	if year==2017  	{ 
	rename sni2 sni
	*drop new_sni
	}
	
	
	save  "new_miljoskydd_`a'.dta" , replace
	local a= `a' + 1
	
	
}


*Preparation for year 2019
use  "miljoskydd_2019.dta" , clear
	
	 destring, replace
	
	capture drop sni
	destring, replace
	g year=2019
	*drop year_1
	rename *, lower
	save  "new_miljoskydd_2019.dta" , replace
	

delete  "new_miljoskydd_2018.dta" 

clear 

*Append data
 use "new_miljoskydd_2002.dta"
local a =2003
 
while `a'<2018 {
	append using  "new_miljoskydd_`a'.dta" 
	display `a'
	local a= `a' + 1
	
}


append  using "new_miljoskydd_2019.dta"
g bidnr = kund_lopnr_peorgnr
duplicates drop bidnr year invbtot invbluft, force

collapse (sum) invbtot invbluft invbvatten invbavfall invbovr invftot invfluft invfvatten invfavfall invfovr intdukost luftdukosti vattendukosti avfalldukosti ovrdukosti extdukost luftdukoste vattendukoste avfalldukoste ovrdukoste madmkost foukost ovrkost personal invbovrigt invfovrigt nvbvatten invbbio invbmark invfbio invfmark markdukosti biodukosti ovrigtdukosti markdukoste biodukostee biodukoste sni , by(bidnr year )

*Collapse data to eliminate potential duplicates

duplicates drop bidnr year , force 

label var invbtot "abatem invest tot"
label var invftot "prev invest tot"
label var invfluft "prev invest luft"
label var invbluft "abatem invest luft"
label var foukost "R and D env"

label var luftdukoste "costs air extern"
label var luftdukosti "costs air intern"

g sni_02 = sni if year<=2007
g sni_07 = sni if year>2007
drop sni

egen total_air=rowtotal( invfluft invbluft luftdukosti luftdukoste)


*total external costs for abatement 
egen DU_ext_tot=rowtotal(  luftdukoste vattendukoste avfalldukoste ovrdukoste markdukoste biodukostee biodukoste )

*total investments behandlande
egen inv_b_tot=rowtotal( invbluft invbvatten invbavfall invbovr)
save "new_miljoskydd_stack.dta" , replace
*/


use "new_miljoskydd_stack.dta" , replace

